export function useQueryTimer() {
    let timerId: any = null
    let stopFlag = false
    let handlerReader:any = null
    let timerDur = 1000

    const start = (handler:any, dur = 1000, render = true) => {
        clear()
        stopFlag = false
        handlerReader = handler
        timerDur = dur
        play(render)
    }

    const play = (render = true) => {
        if (!handlerReader) return
        timerId = setInterval(() => {
            if (!stopFlag) handlerReader()
        }, timerDur)
        if (render) handlerReader()
    }

    const stop = () => {
        stopFlag = true
        clear()
    }

    const clear = () => {
        clearInterval(timerId)
        timerId = null
    }

    return { start, stop, clear, play }
}
